﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chbl.DAL;
using Chbl.Models;

namespace Chbl.Controllers
{    
    public class ArticleController : Controller
    {
        private readonly ChblDb _db;

        public ArticleController(ChblDb db)
        {
            _db = db;
        }

        public ViewResult Index()
        {
            return View(_db.Articles.OrderByDescending(a => a.Date));
        }

        public ViewResult Details(int id)
        {
            Article article = _db.Articles.Find(id);
            return View(article);
        }

        [Authorize(Roles = "admin")]
        public ActionResult Create()
        {
            return View();
        } 

        [HttpPost]        
        public ActionResult Create(Article article)
        {
            article.Date = DateTime.Now;
            if (ModelState.IsValid)
            {
                try
                {
                    _db.Articles.Add(article);
                    _db.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        }
                    }
                }
                return RedirectToAction("Index");  
            }

            return View(article);
        }

        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            Article article = _db.Articles.Find(id);
            return View(article);
        }

        [HttpPost]
        public ActionResult Edit(Article article)
        {
            article.Date = DateTime.Now;
            if (ModelState.IsValid)
            {
                try
                {
                    _db.Entry(article).State = EntityState.Modified;
                    _db.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        }
                    }
                }
                return RedirectToAction("Index");
            }
            return View(article);
        }

        [Authorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {
            Article article = _db.Articles.Find(id);
            return View(article);
        }

        //
        // POST: /ArticleEditor/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Article article = _db.Articles.Find(id);
            _db.Articles.Remove(article);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}